package com.caiju.health.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.caiju.health.dto.OrderSettingDTO;
import com.caiju.health.service.IOrderService;
import com.caiju.health.service.IOrderSettingService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;

@Component
public class CaijuEasyExcelListener extends AnalysisEventListener<OrderSettingDTO> {

    private ArrayList<OrderSettingDTO> orderSettingDTOS = new ArrayList<>();

    @Resource
    private IOrderSettingService orderSettingService;

    @Override
    public void invoke(OrderSettingDTO data, AnalysisContext analysisContext) {
        System.out.println(data + "===========");
//        1.解析一行，存一行的数据
        orderSettingDTOS.add(data);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println("解析完成！");
//        保存到数据库
        orderSettingService.addBatchOrderSetting(orderSettingDTOS);

//        清空集合
        orderSettingDTOS.clear();
    }
}
